<?php
// 用户模型
class SchoolModel extends CommonModel {
	public $_validate	=	array(
		array('sName','require','单位名称必须'),
		array('sName','','单位名称已经存在',self::EXISTS_VAILIDATE,'unique',self::MODEL_INSERT),
		array('sType','require','单位类型必须'),
		array('sStaffCount','number','单位人数必须为数字',2),
		array('sStudentCount','number','学生人数必须为数字',2),
	);

	public $_auto		=	array(
		array('update_time','time',self::MODEL_UPDATE,'function'),
	);
	
	//在新增单位的同时，在不动产表里同时新增一个相应单位id的数据，不动产的管理没有新增，只有修改删除
	protected function _after_insert($data,$options){
		$data0['dwid'] = $data['id'];
		
		$bdc = M('bdc');
		//$data0['dwid'] = $data['id'];
		$bdc->add($data0);
		
		$aqxx = M('aqxx');
		//$data1['dwid'] = $data['id'];
		$aqxx->add($data0);
		
		$st = M('st');
		//$data2['dwid'] = $data['id'];
		$st->add($data0);
		
		$zzwy = M('zzwy');
		//$data3['dwid'] = $data['id'];
		$zzwy->add($data0);
		
		$nh = M('nh');
		//$data4['dwid'] = $data['id'];
		$nh->add($data0);
		$school->where('dwid='.$data['id'])->setField('nd',date('Y'));//并将年度自动设置为今年
		
		//新增单位时自动将排序号设为id号
		$school = M('School');
		$school->where('id='.$data['id'])->setField('sortid',$data['id']);
	}
	protected function _after_delete($data,$options){
		$bdc = M('bdc');
		$bdc->where('dwid in ('.implode(",",$options['where']['id'][1]).')')->delete();
		
		$aqxx = M('aqxx');
		$aqxx->where('dwid in ('.implode(",",$options['where']['id'][1]).')')->delete();
		
		$st = M('st');
		$st->where('dwid in ('.implode(",",$options['where']['id'][1]).')')->delete();
		
		$zzwy = M('zzwy');
		$zzwy->where('dwid in ('.implode(",",$options['where']['id'][1]).')')->delete();
		
		$nh = M('nh');
		$nh->where('dwid in ('.implode(",",$options['where']['id'][1]).')')->delete();
	}
	
}
?>